package com.sudo.api.config;

import com.sudo.common.component.ExtraProperties;
import com.sudo.api.interceptor.ApiRateLimiterInterceptor;
import com.sudo.api.interceptor.AuthorizationInterceptor;
import com.sudo.api.interceptor.TraceIdInterceptor;
import com.sudo.api.interceptor.UserTokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 为道日损 2020-04-24 17:40
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Resource
    private ExtraProperties extraProperties;
    @Resource
    private UserTokenInterceptor userTokenInterceptor;
    @Resource
    public AuthorizationInterceptor authorizationInterceptor;
    @Resource
    public TraceIdInterceptor traceIdInterceptor;
    @Resource
    private ApiRateLimiterInterceptor apiRateLimiterInterceptor;

    /**
     *实现静态资源的映射
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 图片，文件空间，还可以当做静态页面空间（css,js,html,png...）
        registry.addResourceHandler("/fileSpace/**")
                //映射swagger2 ,后面的/要带上
                .addResourceLocations("classpath:/META-INF/resources/")
                //映射本地静态资源 ,后面的/要带上
                .addResourceLocations("file:"+extraProperties.getFileSpace());
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(apiRateLimiterInterceptor).addPathPatterns("/**")
                .excludePathPatterns("/sysUser/isUserTokenRight");
        registry.addInterceptor(traceIdInterceptor).addPathPatterns("/**");
        registry.addInterceptor(userTokenInterceptor).addPathPatterns("/**")
                // 登录了才能访问资源文件 // TODO fileSpace 还需要做一个自己访问自己文件的拦截器，不能访问别人的文件
                .excludePathPatterns(userTokenSysUserList())
                .excludePathPatterns("/doc.html");
        registry.addInterceptor(authorizationInterceptor).addPathPatterns("/**")
                .excludePathPatterns(authSysUserList())
                .excludePathPatterns("/doc.html");
    }

    private List<String> userTokenSysUserList(){
        List<String> list = new ArrayList<>();
        list.add("/sysUser/userLogin");
        list.add("/sysUser/logout");
        list.add("/sysUser/isUserTokenRight");
        list.add("/sysUser/generateImageCode");
        list.add("/sysUser/userRegister");
        list.add("/sysUser/postEmailCode");
        return list;
    }

    private List<String> authSysUserList(){
        List<String> list = new ArrayList<>();
        list.add("/sysUser/userLogin");
        list.add("/sysUser/logout");
        list.add("/sysUser/isUserTokenRight");
        list.add("/sysUser/generateImageCode");
        list.add("/sysUser/userRegister");
        list.add("/sysUser/postEmailCode");
        return list;
    }
}
